// let str = "I'm?***driving*??*to*?beijing*?**after*breakfaset";
// const reg1=new RegExp(/(\?[a-z]{1})/g);
// const reg2=new RegExp(/([\?,\*]+)/g);
// str = str.replace(reg1,(v, ...args)=>{
//     return args[0].substring(1, 2).toUpperCase();
// }).replace(reg2," ");


// 不可用javascript控制循环的方法和关键字，最终生成[3,6,9]这种数组

// const length=10;
// const step=3;
// const reduce = (step, length, arr=[]) => {
//     let lastVal = arr.slice(-1);
//     let value = lastVal.length>0 ? lastVal[0] : 0;
//     if(arr.length<length){
//         arr = [...arr, value+step];
//         console.log(arr)
//         return reduce(step, length, arr);
//     }else{
//         return arr;
//     }
// }
// console.log(reduce(step,length));


class Scheduler{
    constructor(){
        this.tasks = [];
        this.timer = null;
    }
    addTask(task){
        this.tasks.push(task);
    }
    runTask(){
        this.tasks.forEach((item)=>{
            let date = new Date();
            if(date.getHours() === item.time){
                item.task();
            }
        })
    }
    start(){
        if(this.timer){
            return;
        }
        this.timer = setInterval(()=>{
            this.runTask();
        },1000*60*60);
        this.runTask()
    };
}
const scheduler = new Scheduler();
scheduler.addTask({
    time: 8,
    task: ()=>{
        console.log("起床");
    }
});
scheduler.addTask({
    time: 9,
    task: ()=>{
        console.log("刷牙");
    }
});
scheduler.addTask({
    time: 10,
    task: ()=>{
        console.log("上班");
    }
});
scheduler.addTask({
    time: 22,
    task: ()=>{
        console.log("睡觉");
    }
});
scheduler.start();